import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import Login from '@/views/Login.vue'
import Layout from '@/components/Layout.vue'

// 懒加载组件
const Dashboard = () => import('@/views/Dashboard.vue')
const UserManagement = () => import('@/views/UserManagement.vue')
const DoctorManagement = () => import('@/views/DoctorManagement.vue')
const DepartmentManagement = () => import('@/views/DepartmentManagement.vue')
const RegistrationManagement = () => import('@/views/RegistrationManagement.vue')
const ScheduleManagement = () => import('@/views/ScheduleManagement.vue')
const MedicalRecords = () => import('@/views/MedicalRecords.vue')
const PrescriptionManagement = () => import('@/views/PrescriptionManagement.vue')
const MedicineManagement = () => import('@/views/MedicineManagement.vue')
const AnnouncementManagement = () => import('@/views/AnnouncementManagement.vue')
const WardsManagement = () => import('@/views/WardsManagement.vue')

// 管理端路由
let adminRoutes = [
  {
    path: 'dashboard',
    name: 'Dashboard',
    component: Dashboard,
    meta: {
      title: '数据概览 - 医院管理系统',
      requiresAuth: true
    }
  },
  // 用户管理
  {
    path: 'users',
    name: 'UserManagement',
    component: UserManagement,
    meta: {
      title: '用户管理 - 医院管理系统',
      requiresAuth: true
    }
  },
  // 医生管理
  {
    path: 'doctors',
    name: 'DoctorManagement',
    component: DoctorManagement,
    meta: {
      title: '医生管理 - 医院管理系统',
      requiresAuth: true
    }
  },
  // 科室管理
  {
    path: 'departments',
    name: 'DepartmentManagement',
    component: DepartmentManagement,
    meta: {
      title: '科室管理 - 医院管理系统',
      requiresAuth: true
    }
  },
  // 挂号管理
  {
    path: 'registrations',
    name: 'RegistrationManagement',
    component: RegistrationManagement,
    meta: {
      title: '挂号管理 - 医院管理系统',
      requiresAuth: true
    }
  },
  // 排班管理
  {
    path: 'schedules',
    name: 'ScheduleManagement',
    component: ScheduleManagement,
    meta: {
      title: '排班管理 - 医院管理系统',
      requiresAuth: true
    }
  },
  // 就诊管理
  {
    path: 'medical-records',
    name: 'MedicalRecords',
    component: MedicalRecords,
    meta: {
      title: '就诊记录 - 医院管理系统',
      requiresAuth: true
    }
  },
  // 药方管理
  {
    path: 'prescriptions',
    name: 'PrescriptionManagement',
    component: PrescriptionManagement,
    meta: {
      title: '药方管理 - 医院管理系统',
      requiresAuth: true
    }
  },
  // 药品管理
  {
    path: 'medicines',
    name: 'MedicineManagement',
    component: MedicineManagement,
    meta: {
      title: '药品管理 - 医院管理系统',
      requiresAuth: true
    }
  },
  // 病房管理
  {
    path: 'wards',
    name: 'WardsManagement',
    component: WardsManagement,
    meta: {
      title: '病房管理 - 医院管理系统',
      requiresAuth: true
    }
  },
  // 公告管理
  {
    path: 'announcements',
    name: 'AnnouncementManagement',
    component: AnnouncementManagement,
    meta: {
      title: '公告管理 - 医院管理系统',
      requiresAuth: true
    }
  },
]

// 医生端路由

// 用户端路由



const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '登录 - 医院管理系统'
    }
  },
  {
    path: '/change-password',
    name: 'ChangePassword',
    component: () => import('@/views/ChangePassword.vue'),
    meta: {
      title: '修改密码',
      requiresAuth: true
    }
  },
  {
    path: '/',
    component: Layout,
    meta: {
      requiresAuth: true
    },
    children: adminRoutes,
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  }

  // 检查是否需要认证
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 使用Pinia store检查登录状态
    const userStore = useUserStore()
    if (!userStore.isLoggedIn) {
      // 没有登录信息，跳转到登录页
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router 